
import json
import os
# pro_info = {}
# pro_info = {"苹果":{"pro_price":5, "pro_stock":200}}
# with open("test.json","w") as fp:
#       fp.write(json.dumps(pro_info))
# 检查文件是否存在
file_path = "test.json"
if os.path.exists(file_path):
    with open(file_path, "r", encoding="utf-8") as fp:
        pro_info = json.load(fp)
else:
      print("文件不存在，将创建新文件")
      with open(file_path,"w",encoding="utf-8") as fp:
            json.dump({}, fp)  # 创建一个空的JSON对象
      pro_info = {}

def pro_add():
      pro_name = input("请输入商品的名字:")
      if pro_name in pro_info:
            print("商品已存在，不能添加该商品")
      else:
            pro_price = input("请输入商品的价格:")
            pro_stock = input("请输入商品的库存:")
            pro_info[pro_name] = {"pro_price": pro_price, "pro_stock": pro_stock}
def pro_show():
      pro_name = input("请输入商品的名字:")
      if pro_name in pro_info:
            print(f"{pro_name}:价格:{pro_info[pro_name]['pro_price']},库存:{pro_info[pro_name]['pro_stock']}")
      else:
            print("商品不存在")
def pro_delete():
      pro_name = input("请输入要删除的商品名字：")
      if pro_name in pro_info:
            del pro_info[pro_name]
            print("商品删除成功")
      else:
            print("商品不存在，无法删除")
def pro_modify():
      pro_name = input("请输入要修改的商品名字：")
      if pro_name in pro_info:
            pro_price = input("请输入要修改的商品价格：")
            pro_stock = input("请输入要修改的商品库存：")
            pro_info[pro_name] = {"pro_price": pro_price, "pro_stock": pro_stock}
            print("商品修改成功")
      else:
            print("商品不存在，无法修改")


# pro_info = {"苹果":{"pro_price":5, "pro_stock":200}}
while 1:
      print("输入1商品添加")
      print("输入2查看商品")
      print("输入3删除商品")
      print("输入4修改商品")
      print("输入5退出")
      choice = input("请输入你的选择:")
      if choice == "5":
            break
      if choice == "1":
            pro_add()
      elif choice == "2":
            pro_show()
      elif choice == "3":
            pro_delete()
      elif choice == "4":
            pro_modify()
      else:
            print("无效输入，请重新输入")

with open("test.json", "w") as fp:
      fp.write(json.dumps(pro_info))
